define([
    "UtilDir/util",
    "UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
    "UtilDir/fileupload/singleFileUpload",
    "Date", "DateCN", "css!DateCss",
    "EasyUI", "EasyUI-lang"
], function (Util, Tool, SelectOrg, SearchBlock,SFU) {

    var sysPath = getServer() + "/static/app/hddc/hddccjfilemanage";

    var hddcFileManageFormHtml = sysPath + "/views/hddcFileManageForm.html";
    var getHddcFileManageController = function () {
        return getServer() + "/hddc/hddcFileManages";
    };

    /**
     * 页面初始化
     */
    var init = function () {
        
        uploadFile();
        createHddcFileManageGrid();
        initSearchBlock();
        queryBtnBind();
        initcreateProvince();
        // initFileUpload()
    };

    var initFileUpload = function () {
        var settings = {
            placeAt: "singleFileUpload",
            allowMC:false,
            // accept: FILE_TYPES.imagePdfVideo, //作品文件传图片、pdf、视频
            // fileSizeLimit: 200*1024*1024,
            data: $("#fileId").val(),
            onUploadSuccess: function (file) {
                console.log(file);
                // $("#fileId").val(file.attachId);
            },
            onDeleteSavedFile: function (file) {
                console.log(file);
                //调用修改接口，将这一条数据的文件id置空
                // $("#fileId").val("");
                // var data = Tool.serialize("sfuForm");
            },
            onBeforeFileQueued: function (file) {
                console.log(file);
                // 选择完文件后（点击上传之前）的回调
            }
        };
        SFU.init(settings);
    }

    var initSearchBlock = function(){
        SearchBlock.init("searchBlock");
    };

    var queryBtnBind = function(){
        $("#btnSearch").click(function () {
            createHddcFileManageGrid();
        });
        $("#btnReset").click(function () {
            $("#provinceSelect").val("");
            $("#citySelect").val("");
            $("#areaSelect").val("");
            $("#filenumber").val("");
        });
    };

    var initcreateProvince = function () {
        var html = "";
        $("#citySelect").append(html);
        $("#areaSelect").append(html);
        $.ajax({
            url: getServer() + "/divisions/root/subdivisions",
            contentType: "application/json",
            type: "get",
            success: function (data) {
                $.each(data, function (idx, item) {
                    html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                });
                $("#provinceSelect").append(html);
            }
        });
        $("#provinceSelect").change(function () {
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#citySelect option").remove();
            $("#citySelect").append(html);
            $("#areaSelect option").remove();
            $("#areaSelect").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            if ($(this).val() == "北京市" || $(this).val() == "天津市" || $(this).val() == "上海市" || $(this).val() == "重庆市") {
                $("#citySelect").append("<option value='" + $(this).val() + "' exid='" + divisionId + "'>" + $(this).val() + "</option>");
                return;
            }
            $.ajax({
                url: getServer() + "/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#citySelect").append(html);
                }
            });
        });
        $("#citySelect").change(function () {
            if ($(this).val() == "") return;
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#areaSelect option").remove();
            $("#areaSelect").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            $.ajax({
                url: getServer() + "/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#areaSelect").append(html);
                }
            });
        });
    }

    var uploadFile = function () {
        $("#uploadBtn").on("click", function () {
            var uploadinput = document.getElementById("uploadFile");
            var filenumber = document.getElementById("filenumber");
            if (uploadinput.value == "") {
                Util.alert("上传前请先选择文件!");
                return;
            }
            if (filenumber.value == "") {
                Util.alert("上传前请输入文件编码!");
                return;
            }
            var formData = new FormData();
           
            formData.append("file", uploadinput.files[0]);
            // 追加文件编号
            formData.set("filenumber",filenumber.value);
            
            $.ajax({
                url: getServer() + "/sys/getLoginUsers",
                contentType:"application/json",
                type: "get",
                success: function (data1) {
                    // 追加上传者
                    formData.set("authors",data1);
                    $.ajax({
                        url: getHddcFileManageController() + "/upload",
                        data: formData,
                        processData: false, //因为data值是FormData对象，不需要对数据做处理。
                        contentType: false,
                        type: "POST",
                        success: function (data) {
                            Util.alert(data);
                            uploadinput.value = "";
                            filenumber.value = "";
                            createHddcFileManageGrid();
                        }
                    });
                }
            });

        });
        // console.log(document.getElementById("fileIdCondition").files[0].name)
    };
    var createHddcFileManageGrid = function () {
        $("#hddcFileManageGrid").datagrid({
            url: getHddcFileManageController() + "/queryHddcFileManages",
            method: "GET",
            fitColumns: true,
            autoRowHeight: false,
            columns: [[
                {field:"ck",checkbox:true},
                {
                    field: 'province',
                    title: '省',
                    width: '12%',
                    align: 'center',
                },
                {
                    field: 'city',
                    title: '市',
                    width: '12%',
                    align: 'center',
                },
                {
                    field: 'area',
                    title: '县',
                    width: '12%',
                    align: 'center',
                },
                {
                    field: 'fileId',
                    title: '文件编号',
                    width: '12%',
                    align: 'center',
                },
                {
                    field: 'fileName',
                    title: '文件名称',
                    width: '12%',
                    align: 'center',
                },
                {
                    field: 'createTime',
                    title: '上传日期',
                    sortable:true,
                    width: '12%',
                    align: 'center',
                    // formatter:function(value,rowData,rowIndex){
                    //     if (rowData.updateTime == null || rowData.updateTime == '')
                    //         return rowData.createTime;
                    //     else return rowData.updateTime;
                    // }

                },
                {
                    field: 'extends1',
                    title: '上传人',
                    width: '12%',
                    align: 'center',
                },
                {
                    field: 'deal',
                    title: '操作',
                    width: '12%',
                    align: 'center',
                    formatter: function (value, rowData, rowIndex) {
                        return '<a href="#" style="margin-right: 20px" onclick="downloadFile(\'' + rowData.fileName + '\');">下载</a>'
                            + '<a href="#" onclick="deleteFile(\'' + rowData.uuid + '\');">删除</a>'
                    }
                }
            ]],
            toolbar: [
                {
                    text:"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp*请在活断层分析与管理子系统上传1:5万活断层分布图、1:25万构造图的图片、说明书及报告成果数据",
                }
            ],
            queryParams: {
                // fileId: $("#fileIdCondition").val(),
                fileName: $("#fileNameCondition").val(),
                province: $("#provinceSelect").val(),
                city: $("#citySelect").val(),
                area: $("#areaSelect").val(),
                fileId:$("#filenumber").val()
            },
            pagination: true,
            pageSize: 10
        });
    };

    window.deleteFile = function(id) {
        console.log("shanchu");
        Util.confirm("是否要删除选中的数据?", function() {
            $.ajax({
                url: getHddcFileManageController()+"/delete",
                contentType:"application/json",
                data:  id,
                type: "post",
                success: function (data) {
                    createHddcFileManageGrid();
                }
            });
        }, function() {
            return;
        });

    };


    window.downloadFile = function(name) {
        window.location.href = getHddcFileManageController() + "/download/" + name;
        // $.ajax({
        //     url: getHddcFileManageController() + "/download/" + name ,
        //     type: "get",
        //     success: function (data) {
        //     }
        // });

    };


    return {
        init: init
    };
});
